package com.wellsoft.mobileoa.utils.action;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;

import com.wellsoft.mobileoa.FuncPageActivity;
import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_HttpGet_Request;
import com.wellsoft.mobileoa.wsmodel.WSOA_HttpGet_Response;
import com.wmobile.utils.BaseObject;
import com.wmobile.utils.ServiceResponse;

public class HttpGetAciton extends BaseAction {

	public HttpGetAciton(WSOA_Action action, BaseView baseView) {
		this.action = action;
		this.baseView = baseView;
	}

	@Override
	public ServiceResponse excuteAction() {
		if (!SystemConfig.stringIsNullOrEmpty(action.getGoBackUrl())) {
			SystemConfig.setGoBackUrl(action.getGoBackUrl());
			SystemConfig.setGoBackTargetUrl(baseView.getUrl());
		}

		WSOA_HttpGet_Request request = new WSOA_HttpGet_Request();
		request.setUrl(action.getUrl());
		request.setCookies(SystemConfig.getCookies());

		Map<String, BaseObject> httpRequests = new HashMap<String, BaseObject>();

		httpRequests.put("request", request);

		if (actionNo == 1) {
			ServiceResponse httpGetResponse = SystemConfig.getService()
					.CallserviceOnMainThread(httpRequests, "WSOA_HttpGet",
							WSOA_HttpGet_Response.class);
			if (!httpGetResponse.getExeFlag()) {
				return httpGetResponse;
			}
			WSOA_HttpGet_Response response = (WSOA_HttpGet_Response) httpGetResponse
					.getResponseData();
			if (response.getExeFlag() == 0) {
//				baseView.getBaseActivity().showMessage(response.getMessage());
				return new ServiceResponse(false, response.getMessage());
			}
//			if (response.getExeFlag() == 2) {
//				boolean isSuccess = false;
//				for (int i = 0; i < 2; i++) {
//					if (SystemConfig.reLogin()) {
//						isSuccess = true;
//						break;
//					}
//				}
//				if (isSuccess) {
//					return excuteAction();
//				} else {
//					BaseActivity.returnLoginActivity();
//					return new ServiceResponse(false);
//				}
//			}
			if (response.getExeFlag() == 2) {
				if (SystemConfig.reLogin()) {
					return excuteAction();
				} else {
					BaseActivity.returnLoginActivity();
					return new ServiceResponse(false);
				}
			}
			return new ServiceResponse(true, response.getNextPage());
			// baseActivity.getBaseView().addNextPage(response.getNextPage());
		} else {
			ServiceResponse httpGetResponse = null;
			if (isMainThread) {
				httpGetResponse = SystemConfig.getService()
						.CallserviceOnMainThread(httpRequests, "WSOA_HttpGet",
								WSOA_HttpGet_Response.class);
			} else {
				httpGetResponse = SystemConfig.getService().Callservice(
						httpRequests, "WSOA_HttpGet",
						WSOA_HttpGet_Response.class);
			}
			if (!httpGetResponse.getExeFlag()) {
				return httpGetResponse;
			}
			WSOA_HttpGet_Response response = (WSOA_HttpGet_Response) httpGetResponse
					.getResponseData();
			if (response.getExeFlag() == 0) {
//				baseView.getBaseActivity().showMessage(response.getMessage());
				return new ServiceResponse(false, response.getMessage());
			}
			if (response.getExeFlag() == 2) {
				if (SystemConfig.reLogin()) {
					return excuteAction();
				} else {
					BaseActivity.returnLoginActivity();
					return new ServiceResponse(false);
				}
			}

			Intent intent = new Intent();
			intent.setClass(baseView.getBaseActivity(), FuncPageActivity.class);

			intent.putExtra("page", response.getNextPage());
			intent.putExtra("url", action.getUrl());
			baseView.getBaseActivity().NewActivity(intent);
		}
		return new ServiceResponse(true);
	}

}
